#!/usr/bin/perl
use strict;  

use DBI;
use CGI;
use TTraker;

& print_header;
my $dbh;
& initialize_dbi;

my $job;

my $form_ref = &parse_input;
my %FORM_DATA = %$form_ref; 
my $name = $FORM_DATA{'empName'};
my $num = $FORM_DATA{'empNum'};
my $job = $FORM_DATA{'empJob'};
my $empLoc = $FORM_DATA{'empLoc'};
my $cliID = $FORM_DATA{'cliID'};
my $loc = $FORM_DATA{'locNum'};
my $cliName = $FORM_DATA{'cliName'};
my ($msg, $msg2, $cli_count, $task_count);

if ( $cliID eq  "" || $cliName eq "" || $loc eq "") {
	$msg = "NEW CLIENT NOT ADDED!<br/>Require: Client ID, Location Number and Client Name!"; 
	trak_03_add_new_client_message ($num, $name, $job,$empLoc,$msg);
} else {
	$cli_count = get_client_count ($cliID);
	if ( $cli_count == 0 ) {
		$task_count = add_new_client ($cliID, $loc, $cliName);
		$msg = "$cliID added to clients table and $task_count tasks added.<br/>";
  		adm_activity_screen_from_pm ($num, $name, $job,$empLoc,$msg);
	} elsif ( $cli_count == 1 ) {
		$msg = "NEW CLIENT NOT ADDED!<br/>$cliID already exists in the data base!"; 
		trak_03_add_new_client_message ($num, $name, $job,$empLoc,$msg);
	}
}

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################


sub trak_03_add_new_client_message
{
my $num = $_[0];
my $name = $_[1];
my $job = $_[2];
my $empLoc = $_[3];
my $msg = $_[4];

print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_adm_add_new_client.pl</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  &nbsp&nbsp&nbsp&nbsp&nbsp
  <div id = "box5_1"> 
  <h3>Add New Client</h3>
  </div>
<div id="box1">
  &nbsp&nbsp&nbsp&nbsp&nbsp Client ID: <br/><br/>
  &nbsp&nbsp&nbsp&nbsp&nbsp Location #: <br/><br/>
  &nbsp&nbsp&nbsp&nbsp&nbsp Client Name: <br/><br/>
</div>
<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_03_add_new_client.pl" method="post">
  <input type="hidden" name="empName" value='$name'/>
  <input type="hidden" name="empNum" value='$num'/>
  <input type="hidden" name="empJob" value='$job'/>
  <input type="hidden" name="empLoc" value='$empLoc'/>
  <input type="text" size=12 value = "$cliID" name=cliID />&nbsp&nbsp
  <br/><br/>
   <input type="text" size=6 value = "$loc" name=locNum />&nbsp&nbsp
   <br/><br/>
   <input type="text" size=30 value = "$cliName" name=cliName />&nbsp&nbsp
   <br/><br/><br/>
   <input type="submit" value="Add New Client">
   <br/><br/><br/>
   <a href="${cgi_ref}trak_00_adm_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
   <br/><br/><br/>
   <b id="warn">$msg</b>
 </form>
 </div>
 </body>
 </html>
ENDHEADER

}


